package searchteam.client.ui.widgets;

import java.util.ArrayList;

import searchteam.client.beansMirrors.PersonMirror;
import searchteam.client.services.SearchTeamServices;
import searchteam.client.services.SearchTeamServicesAsync;
import searchteam.client.ui.SearchTeamUI;
import searchteam.client.ui.callbacks.getAllPersonsCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.ListBox;

public class PersonListBox extends ListBox implements Display {
	
	private ArrayList<PersonMirror> persons;
	private PersonMirror personToSelect;
	private boolean thereIsAPersonToSelect;
	
	SearchTeamServicesAsync services = GWT.create(SearchTeamServices.class);
	
	public PersonListBox() {
		thereIsAPersonToSelect = false;
		persons = new ArrayList<PersonMirror>();
		this.setVisibleItemCount(1);
		
		services.getAllPersons(new getAllPersonsCallback(this));
	}
	
	public PersonListBox(PersonMirror person) {
		thereIsAPersonToSelect = true;
		personToSelect = person;
		persons = new ArrayList<PersonMirror>();
		this.setVisibleItemCount(1);
		
		services.getAllPersons(new getAllPersonsCallback(this));
	}

	public void addMirror(MirrorDisplay mirror) {
		persons.add(((PersonMirrorDisplay)mirror).getBean());
		this.addItem(mirror.getText());
		if(thereIsAPersonToSelect && mirror.getId() == personToSelect.getId()) {
			this.setSelectedIndex(this.getItemCount() - 1);
		}
	}

	public void clearMirrors() {
		persons.clear();
		this.clear();
	}

	public SearchTeamUI getMainUI() {
		return null;
	}
	
	public PersonMirror getSelectedPerson() {
		return persons.get(this.getSelectedIndex());
	}

}
